package main;

/*
 *TODO:
 *1) modify to not extend JFrame
 *2) move action listeners up one level 
 *3) Updating JTable?
*/

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.event.TableModelEvent;

public class MainWindow extends JFrame {
    private DBConnect connection;
	private JPanel panel;
	private String typed;
	private String[] partStrings = { "resistor", "capacitor"};
	private JTable table;
	private JPanel mainWorkPlace;
	private JComboBox partList;
	
	
	public MainWindow (String title, DBConnect connect){
		//window setup
		super(title);
		setSize(800,600);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setLayout (new BorderLayout());
		
		// set connection
		connection = connect;
		
		// creates the main work panel
		mainWorkPlace = new JPanel();
		mainWorkPlace.setLayout(new BorderLayout());
		add(mainWorkPlace, BorderLayout.CENTER);
		
		// TOP Menu
		JPanel northMenu = new JPanel();
		northMenu.setLayout(new FlowLayout());
		add(northMenu, BorderLayout.NORTH);
		
		// COMBO box
		ap ap1 = new ap();
		partList = new JComboBox(partStrings);
		partList.setSelectedIndex(1);
		partList.addActionListener(ap1);
		northMenu.add(partList);	
		
		
		
	}

	private class ap implements ActionListener{
	@Override
	public void actionPerformed(ActionEvent e) {
		
		
		String x = String.valueOf(partList.getSelectedItem());
			
		table = connection.getData(x);
		
		table.setModel(table.getModel());
		//TableModelEvent tblEvent = new TableModelEvent(table.getModel());
		
		
		table.setAutoCreateRowSorter(true);
		mainWorkPlace.add(table.getTableHeader(), BorderLayout.PAGE_START);
		mainWorkPlace.add(table, BorderLayout.CENTER);
		
		
		mainWorkPlace.validate();
		mainWorkPlace.repaint();
	}
	}

	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}
	
}
